﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Data;
using System.Collections.Generic;

namespace LiveEduWizard
{
    public class WeekGroupConverter : IValueConverter
    {
        public class WeekGroup : IEnumerable<TimetableViewModel>
        {
            public WeekGroup(int week, int month, int year)
            {
                this.Items = new List<TimetableViewModel>();

                Week = week;
                Month = month;
                Year = year;

                Title = string.Format("{0}, {1}", CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[month - 1], year);
            }

            public string Title
            {
                get;
                set;
            }

            public IList<TimetableViewModel> Items
            {
                get;
                set;
            }

            public int Week { get; private set; }

            public int Month { get; private set; }

            public int Year { get; private set; }

            public string MonthYear { get; private set; }

            #region IEnumerable<TimetableViewModel> Members

            public IEnumerator<TimetableViewModel> GetEnumerator()
            {
                return this.Items.GetEnumerator();
            }

            #endregion

            #region IEnumerable Members

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this.Items.GetEnumerator();
            }

            #endregion

            public static int GetMonth(int week, int year)
            {
                DateTime firstDay = new DateTime(year, 1, 1, 12, 0, 0);

                return firstDay.AddDays((week - 1) * 7 - (firstDay.DayOfWeek == DayOfWeek.Sunday ? 6 : ((int)firstDay.DayOfWeek - 1))).Month;
            }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is ObservableCollection<TimetableViewModel>)
            {
                WeekGroup weekGroup = null;
                ObservableCollection<WeekGroup> weekGroups = new ObservableCollection<WeekGroup>();

                foreach (var week in (ObservableCollection<TimetableViewModel>)value)
                {
                    int month = WeekGroup.GetMonth(week.Week, week.Year);

                    if (weekGroup == null || weekGroup.Month != month)
                    {
                        weekGroup = new WeekGroup(week.Week, month, week.Year);
                        weekGroups.Add(weekGroup);
                    }

                    weekGroup.Items.Add(week);
                }

                return weekGroups;
            }
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is ObservableCollection<WeekGroup>)
            {
                ObservableCollection<TimetableViewModel> timetables = new ObservableCollection<TimetableViewModel>();

                foreach (var weekGroup in (ObservableCollection<WeekGroup>)value)
                    foreach (var week in weekGroup.Items)
                        timetables.Add(week);

                return timetables;
            }
            else
                return null;
        }
    }
}
